package com.ncloud.resman.mapper;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.resman.entity.DeviceUnit;

import java.util.List;

import com.ncloud.resman.entity.VirtualDisk;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * Created by shiqiang on 2017/8/14.
 */
@Mapper
public interface DeviceUnitMapper {
    @Insert("insert into res_device_unit(id,device_id, unit_type, type_name, brand_id, brand_name, capacity, amount,use_tag )" +
            " values(#{id},#{deviceId},#{unitType},#{typeName},#{brandId},#{brandName},#{capacity},#{amount},#{useTag})")
    int insertDeviceUnit(DeviceUnit deviceUnit);
    @Update("update res_device_unit set device_id =#{deviceId},brand_id = #{brandId},brand_name=#{brandName},capacity =#{capacity},amount =#{amount}" +
            " where id =#{id}")
    int updateDeviceUnit(DeviceUnit deviceUnit);

    /*删除*/
    @Delete(value = "delete from res_device_unit WHERE device_id = #{0}")
    public int deleteDeviceUnit(Long deviceId);
    
    @Select("SELECT CONCAT(id,'') as id, CONCAT(device_id,'') as device_id, unit_type, type_name, CONCAT(brand_id,'') as brand_id, brand_name, capacity, amount, use_tag "
			+ "FROM res_device_unit where device_id = #{deviceId}")
	@Results({
		@Result(column="id",property="id"),@Result(column="device_id",property="deviceId"),
		@Result(column="unit_type",property="unitType"),@Result(column="type_name",property="typeName"),
		@Result(column="brand_id",property="brandId"),@Result(column="brand_name",property="brandName"),
		@Result(column="capacity",property="capacity"),@Result(column="amount",property="amount"),
		@Result(column="use_tag",property="useTag")})
	List<JSONObject> getDeviceUnitById(Long deviceId);//根据设备id查询设备部件信息
    
    
    @Select("select a.device_id,a.brand_name,a.capacity,a.amount from res_device_unit a ,res_device_base b"
    		+ " where a.device_id=b.device_id and if(#{0} is null,1=1,b.sn_num=#{0})")
	@Results({
		@Result(column="id",property="id"),@Result(column="device_id",property="deviceId"),
		@Result(column="unit_type",property="unitType"),@Result(column="type_name",property="typeName"),
		@Result(column="brand_id",property="brandId"),@Result(column="brand_name",property="brandName"),
		@Result(column="capacity",property="capacity"),@Result(column="amount",property="amount"),
		@Result(column="use_tag",property="useTag")})
	List<DeviceUnit> getDeviceUnitBySnNum(String snNum);//根据设备snNum查询设备部件信息

	/*根据构件类型查询具体类型信息*/

	/*6	CPU
7	内存
8	硬盘
9	电源
10	网卡
11	控制卡
1	普通接口
2	40G接口
3	管理口*/

	@Select("SELECT id, device_id, unit_type, type_name, brand_id, brand_name, capacity, amount, use_tag "
			+ " FROM res_device_unit where device_id=#{0} and unit_type=#{1} ")
	@Results({
			@Result(column="id",property="id"),@Result(column="device_id",property="deviceId"),
			@Result(column="unit_type",property="unitType"),@Result(column="type_name",property="typeName"),
			@Result(column="brand_id",property="brandId"),@Result(column="brand_name",property="brandName"),
			@Result(column="capacity",property="capacity"),@Result(column="amount",property="amount"),
			@Result(column="use_tag",property="useTag")})
	List<DeviceUnit> getDeviceUnitByType(Long deviceId,String unitType);

	/*根据ipmi的ip获取服务器下的硬盘信息*/
    /*select b.* FROM res_device_server a,res_device_unit b where b.device_id=a.device_id and b.unit_type='5' and b.use_tag='1' and a.ipmi_addr ='172.16.0.1' */
	@Select("select b.* FROM res_device_server a,res_device_unit b where b.device_id=a.device_id "
			+ " and b.unit_type=#{1} and b.use_tag='1' and a.ipmi_addr =#{0} ")
	@Results({
			@Result(column="id",property="id"),@Result(column="device_id",property="deviceId"),
			@Result(column="unit_type",property="unitType"),@Result(column="type_name",property="typeName"),
			@Result(column="brand_id",property="brandId"),@Result(column="brand_name",property="brandName"),
			@Result(column="capacity",property="capacity"),@Result(column="amount",property="amount"),
			@Result(column="use_tag",property="useTag")})
	List<DeviceUnit> getDeviceUnitByIpmi(String ipmiAddr,String unitType);//
}
